package com.alibaba.basic.service.impl;
import com.alibaba.basic.query.BaseQuery;
import com.alibaba.basic.service.IBaseService;
import com.alibaba.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T> {

    @Autowired
    private com.alibaba.basic.mapper.BaseMapper<T> BaseMapper;


    @Override
    public void add(T t) {
        BaseMapper.save(t);

    }

    @Override
    @Transactional
    public void del(Long id) {
        BaseMapper.remove(id);

    }

    @Override
    @Transactional
    public void update(T t) {
        BaseMapper.update(t);
    }

    @Override
    public T findById(Long id) {
        return BaseMapper.loadById(id);
    }

    @Override
    public List<T> findAll() {
        return BaseMapper.loadAll();
    }

    @Override
    public PageList<T> queryPage(BaseQuery query) {
        Long totals = BaseMapper.queryCount(query);
        if(totals<1){
            return new PageList<>();
        }
        List<T> data = BaseMapper.queryData(query);
        return new PageList<T>(totals,data);
    }

    @Override
    public void atchDelete(Long[] id) {
        BaseMapper.atchDelete(id);
    }
}
